
package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exceptions.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.entity.PrintTemplate;
import com.scs.application.modules.base.mapper.PrintTemplateMapper;
import com.scs.application.modules.base.request.PrintTemplateQueryRequest;
import com.scs.application.modules.base.service.PrintTemplateService;
import com.scs.application.modules.base.vo.PrintTemplateVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 打印模板配置 service 实现
 *
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class PrintTemplateServiceImpl extends BaseServiceImpl<PrintTemplateMapper, PrintTemplate> implements PrintTemplateService {

    @Override
    public List<PrintTemplateVO> list(PrintTemplateQueryRequest request) {
        List<PrintTemplateVO> list = this.baseMapper.list(request);
        return list;
    }

    @Override
    public IPage<PrintTemplateVO> page(PrintTemplateQueryRequest request) {
        IPage page = this.baseMapper.page(request.getPage(), request);
        return page;
    }

    @Override
    public List<PrintTemplate> findPrintTemplate(PrintTemplateQueryRequest request) {
        List<PrintTemplate> printTemplates = this.baseMapper.selectList(Wrappers.<PrintTemplate>query().eq(StringUtils.isNoneBlank(request.getHospitalId()), "hospital_id", request.getHospitalId())
                .eq(StringUtils.isNoneBlank(request.getBusType()), "bus_type", request.getBusType())
                .eq(StringUtils.isNoneBlank(request.getThisPageSize()), "page_size", request.getThisPageSize()));
        return printTemplates;
    }

    @Override
    public PrintTemplateVO findPrintTemplateVO(PrintTemplateQueryRequest request) {
        List<PrintTemplate> list = findPrintTemplate(request);
        PrintTemplateVO printTemplateVO = new PrintTemplateVO();
        if (list.size() == 1) {
            BeanUtils.copyProperties(list.get(0), printTemplateVO);
        } else if (list.size() > 1) {
            throw new BusinessException("根据条件查询多条配置数据，请检查");
        }
        return printTemplateVO;
    }

    private QueryWrapper<PrintTemplate> getQueryWrapper(PrintTemplateQueryRequest request) {
        return Wrappers.<PrintTemplate>query()
                .eq(StringUtils.isNotBlank(request.getBusType()), "bus_type", request.getBusType())
                .orderByDesc("gmt_modified");
    }

    @Override
    public boolean saveCascadeById(PrintTemplate entity) {
        PrintTemplate printTemplate = this.baseMapper.selectOne(Wrappers.<PrintTemplate>query().eq("hospital_id", entity.getHospitalId())
                .eq("bus_type", entity.getBusType())
                .eq("page_size", entity.getPageSize())
        );
        if (printTemplate != null) {
            throw new BusinessException("模板已经存在，添加失败");
        }
        return this.save(entity);
    }

    @Override
    public boolean updateCascadeById(PrintTemplate entity) {
        PrintTemplate printTemplate = this.baseMapper.selectOne(Wrappers.<PrintTemplate>query().eq("hospital_id", entity.getHospitalId())
                .eq("bus_type", entity.getBusType())
                .eq("page_size", entity.getPageSize())
                .ne("id", entity.getId())
        );
        if (printTemplate != null) {
            throw new BusinessException("模板已经存在，修改失败");
        }
        return this.updateById(entity);
    }
}
